using System;
using System.Linq.Expressions;

namespace Dorm.Helpers
{
    public class LambdaParser
    {
        public static string GetFieldName<T>(Expression<Func<T, object>> expressionTree)
        {
            if (expressionTree.Body is UnaryExpression)
                return ((expressionTree.Body as UnaryExpression).Operand as MemberExpression).Member.Name;
            else
            {
                if ((expressionTree.Body is MemberExpression))
                {
                    if ((expressionTree.Body as MemberExpression).Expression is MemberExpression)
                    {
                        string propertyName = ((expressionTree.Body as MemberExpression).Expression as MemberExpression).Member.Name
                               + "." + (expressionTree.Body as MemberExpression).Member.Name;
                        return propertyName;
                    }
                    else return (expressionTree.Body as MemberExpression).Member.Name;
                }
                return null;
            }
                

            //throw new Exception("Currently only direct scalar properties are suported via lambda expression.");
        }
    }
}
